<?php
class isp_komplain_model extends MY_Model {
	var $model = 'isp_komplain';
	
	function ISPStatus()
	{
		$ls_sql	= "	SELECT	KODE_STATUS value,
							NAMA_STATUS display
					FROM	ISP_KOMPLAIN_STATUS
					ORDER BY NAMA_STATUS";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}

	function GetJSON()
	{
		$ls_sql	= "	SELECT	A.id_komplain,a.address,a.phone,A.kode_komplain,TO_CHAR(A.tgl_komplain,'DD/MM/YYYY') tgl_komplain,
							A.kode_customer,A.komplain,A.status,A.keterangan,B.NAMA,c.nama_status,
							a.jenis_kebutuhan,
							a.jenis_permintaan,
							TO_CHAR(A.tanggal_selesai,'DD/MM/YYYY') tanggal_selesai
					FROM	ISP_KOMPLAIN A
					LEFT JOIN 	ISP_CUSTOMER B ON A.KODE_CUSTOMER=B.KODE_CUSTOMER
					LEFT JOIN isp_komplain_status c ON a.status = c.kode_status
					WHERE	(UPPER(kode_komplain) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(komplain) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'id_komplain' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}

	function GetJSON2()
	{
		$ls_sql	= "	SELECT	*
					FROM	ISP_CUSTOMER
					WHERE	(UPPER(kode_customer) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'kode_customer' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('isp_komplain', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('isp_komplain', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_id_komplain){
		$result = $this->db->get_where('isp_komplain', array('id_komplain' => $as_id_komplain));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_id_komplain)
	{
		$result = $this->db->get_where('isp_komplain', array('id_komplain' => $as_id_komplain));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM isp_komplain WHERE id_komplain = '".$arr_rule['id_komplain']."'"))<=0)
			{
				return $this->error_delete.' [Id: '.$arr_rule['id_komplain']."]<br />";
			}else{
				$this->db->delete('isp_komplain', array('id_komplain' => $arr_rule['id_komplain']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function GetCetak($kode_komplain)
	{
		$result = $this->db->query("SELECT	A.id_komplain,a.address,a.phone,A.kode_komplain,TO_CHAR(A.tgl_komplain,'DD/MM/YYYY') tgl_komplain,
											A.kode_customer,A.komplain,A.status,A.keterangan,B.NAMA,c.nama_status,
											a.jenis_kebutuhan,
											a.jenis_permintaan,
											TO_CHAR(A.tanggal_selesai,'DD/MM/YYYY') tanggal_selesai
									FROM	ISP_KOMPLAIN A
									LEFT JOIN 	ISP_CUSTOMER B ON A.KODE_CUSTOMER=B.KODE_CUSTOMER
									LEFT JOIN isp_komplain_status c ON a.status = c.kode_status
									WHERE A.kode_komplain ='".$kode_komplain."' ");
		return $result->row_array();
	}
}
?>